﻿using System;
using System.Collections.Generic;
using System.Threading;
using csLTDMC;
using InspectionMAC.Function;

namespace InspectionMAC.SystemThread
{
    class OverflowHandlerThread02
    {
        public static bool bAddOverflow = false;
        public static Thread AddOverflow_Thread = null;
        public static List<uint> LPos = new List<uint>();       //--這個記錄比較位置
        public static List<uint> Ldir = new List<uint>();       //--這個記錄比較模式(0為小於等於  /  1為大於等於)，可以直接預設1
        public static List<ushort> LAction = new List<ushort>();  //--這個記錄DO執行模式(正式使用5為一次500us脈波或6為一次1ms脈波)
        public static List<ushort> LActpara = new List<ushort>(); //--這個記錄DO號
        public static List<uint> Laxis = new List<uint>();
        public static List<OverFlowInfo> overFlowInfos = new List<OverFlowInfo>();

        public static void AddOverflow_State()
        {
            bAddOverflow = true;
            AddOverflow_Thread = new Thread(AddOverflow_Run);
            AddOverflow_Thread.IsBackground = true;
            AddOverflow_Thread.Start();
        }

        private static void AddOverflow_Run(object obj)
        {
            while (bAddOverflow)
            {
                if (LocationThread.EncoderCont[0] < 1000)
                {
                    MotionStatus_API.bOverflow = false;
                    ///----在溢位後，將目前未添加的比較位置都一次添加進去----------------------------
                    for (int i = 0; i <= LPos.Count; i++)
                    {
                        UInt32 UPos = LPos[i];
                        ushort Action = LAction[i];
                        ushort Actpara = LActpara[i];
                        MotionStatus_API.AddOverflowLocation(0, 0, UPos, 1, Action, Actpara);
                    }
                    for (int i = 0; i < overFlowInfos.Count; i++)
                    {
                        MotionStatus_API.AddOverflowLocation(0, 0, overFlowInfos[i].LPos, 1, overFlowInfos[i].LAction, overFlowInfos[i].LActpara);
                    }
                    ///---添加後，都把list給清除了，等待下一次溢位時進行記錄-------------------------
                    LPos.Clear();
                    Ldir.Clear();
                    LAction.Clear();
                    LActpara.Clear();
                    Laxis.Clear();

                    overFlowInfos.Clear();
                    ///-------------------------------------------------------------------------------
                    bAddOverflow = false;
                }
                Thread.Sleep(1);
            }
        }
    }
}